package org.lzy.shop.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.response.OrderResponse

class MyOrderAdapter: BaseQuickAdapter<OrderResponse.List, QuickViewHolder>()  {

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_order, parent)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: OrderResponse.List?) {
        item?.let {
            when (item.status) {
                0-> {
                    holder.setText(R.id.tv_order_status, "待付款")
                    holder.setGone(R.id.tv_buy_again, false)
                    holder.setGone(R.id.tv_cancel_order, false)
                }
                1-> {
                    holder.setText(R.id.tv_order_status, "待发货")
                    holder.setGone(R.id.tv_apply_return, false)
                    holder.setGone(R.id.tv_buy_again, false)
                }
                2-> {
                    holder.setText(R.id.tv_order_status, "待收货")
                    holder.setGone(R.id.tv_apply_return, false)
                    holder.setGone(R.id.tv_confirm_deliver_goods, false)
                    holder.setGone(R.id.check_logistics_tv, false)
                    holder.setGone(R.id.tv_buy_again, true)
                }
                3-> {
                    holder.setText(R.id.tv_order_status, "待评价")
                    holder.setGone(R.id.order_comment, false)
                    holder.setGone(R.id.check_logistics_tv, false)
                    holder.setGone(R.id.tv_buy_again, false)
                    holder.setGone(R.id.tv_confirm_deliver_goods, true)

                }
                4-> {
                    holder.setText(R.id.tv_order_status, "交易完成")
                    holder.setGone(R.id.check_logistics_tv, false)
                    holder.setGone(R.id.order_comment, false)
                    holder.setGone(R.id.tv_buy_again, false)
                    holder.setGone(R.id.tv_apply_return, true)
                    holder.setGone(R.id.tv_confirm_deliver_goods, true)

                }
                5-> {
                    holder.setText(R.id.tv_order_status, "退款中")
                    holder.setGone(R.id.check_logistics_tv, false)
                    holder.setGone(R.id.tv_buy_again, false)
                }
                6-> {
                    holder.setText(R.id.tv_order_status, "已退款")
                    holder.setGone(R.id.check_logistics_tv, false)
                    holder.setGone(R.id.tv_buy_again, false)
                }
                7-> {
                    holder.setText(R.id.tv_order_status, "已关闭")
                    holder.setGone(R.id.tv_buy_again, false)
                    holder.setGone(R.id.check_logistics_tv, true)
                    holder.setGone(R.id.order_comment, true)
                    holder.setGone(R.id.tv_apply_return, true)
                    holder.setGone(R.id.tv_confirm_deliver_goods, true)

                }
            }

            val  orderPicAdapter=OrderPicAdapter()
            val  shopPicRv = holder.getView<RecyclerView>(R.id.order_shop_tv)
            shopPicRv.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                adapter=orderPicAdapter
                isNestedScrollingEnabled=true

            }
            //解决背部RV点击无效
            shopPicRv.setOnTouchListener { v, event -> holder.itemView.onTouchEvent(event) }

            if (item.goodsList!!.isNotEmpty()) {
                val goodsSpecList : MutableList<String> = ArrayList()
                for (list in item.goodsList) {
                    if (list.spec!!.images!!.isNotEmpty()) {
                        for (index in 0 until list.spec!!.images!!.size) {
                            goodsSpecList?.addAll(list.spec!!.images!!)
                        }
                    }else{
                        goodsSpecList?.addAll(listOf(list.goodsInfo!!.mainPic))
                    }
                }
                orderPicAdapter.submitList(goodsSpecList)
            }

            holder.setText(R.id.tv_order_number, item.orderNum)
            holder.setText(R.id.tv_goods_num, "共" + item.goodsList?.size + "件")
            val price = item.price
            val count = item.goodsList?.get(0)?.count
            val priceResult = price * count!!
            holder.setText(R.id.tv_total_price, "￥" +priceResult)


        }

    }

}